package com.itheima.train.manager.order.handler.mq;

import com.alibaba.fastjson.JSON;
import com.itheima.train.manager.domain.Order;
import com.itheima.train.manager.domain.OrderHandler;
import com.itheima.train.manager.order.handler.controller.ZhzWebSocketServer;
import com.itheima.train.manager.order.handler.service.TbOrderService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @program: itheima-train-manager-parent
 * @description: 监听创建好的订单, 并且写入数据库
 * @author: zhanghz001
 * @create: 2021-07-06 17:26
 **/
// @Component
public class ZhzOrderHandlerConsumer {
    //日志记录器
    private Logger log = LoggerFactory.getLogger(ZhzOrderHandlerConsumer.class);
    @Autowired
    private TbOrderService orderService;
    @Autowired
    private RabbitmqProducer rabbitmqProducer;
    @Autowired
    private ZhzWebSocketServer zhzWebSocketServer;
    
    //消费产生的订单数据,存入数据库
    @RabbitListener(queues = "syn_gen_order_queue")
    public void orderHandlerConsumer(String orderHandlerJsonInfo, Message message, Channel channel) {
        
        try {
            OrderHandler orderHandler = JSON.parseObject(orderHandlerJsonInfo, OrderHandler.class);
            //--------------------------消费产生的订单数据,存入数据库------------------------------------------------
            
            //调用service进行数据保存
            log.info("【保存订单数据开始了】 ---> " + JSON.toJSONString(orderHandler));
            
            //订单业务逻辑处理时间
            Thread.sleep(20000);
            Order order = orderHandler.getOrder();
            //调用service成的代码完成数据保存操作
            orderService.saveOrder(order);
            
            //发送删除redis的排队信息
            //获取相关参数
            String userId = order.getUserId();
            String trainNum = order.getTrainNum();
            String trainRidingDate = order.getTrainRidingDate();
            String sortedKey = trainNum + ":" + trainRidingDate + ":" + userId;
            rabbitmqProducer.sendMessage(sortedKey, "delete_sorted");
            
            //完成订单信息的推送
            sendOrderInfo(userId, true, String.valueOf(order.getId()));
            
            //给出应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("【保存订单数据结束了】 ---> " + JSON.toJSONString(orderHandler));
            
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【保存订单数据结束了】 ---> " + orderHandlerJsonInfo);
            
            //完成订单信息的推送
        } finally {
            
        }
    }
    
    // 进行消息的推送,链接用户,告知用户下单成功
    public void sendOrderInfo(String userId, boolean result, String orderId) {
        HashMap<String, Object> resultHashMap = new HashMap<>();
        resultHashMap.put("result", result);
        resultHashMap.put("orderId", orderId);
        zhzWebSocketServer.sendMessageToUser(userId, JSON.toJSONString(resultHashMap));
    }
    
}
